package com.example.springbootbook2.service;

import com.example.springbootbook2.enums.BookStatusEnum;
import com.example.springbootbook2.mapper.BookInfoMapper;
import com.example.springbootbook2.model.BookInfo;
import com.example.springbootbook2.model.PageRequest;
import com.example.springbootbook2.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BookInfoService {
    @Autowired
    private BookInfoMapper bookInfoMapper;

    public PageResult getBookInfoByPage(PageRequest pageRequest) {
        List<BookInfo> result;
        Integer total = bookInfoMapper.getTotal();
        try {
            result = bookInfoMapper.getBookInfoByPage(pageRequest.getOffset(),pageRequest.getPageSize());
            if (result != null && !result.isEmpty()) {
                for (BookInfo bookInfo:result) {
                    bookInfo.setStatusCN(BookStatusEnum.getNameByCode(bookInfo.getStatus()).getName());
                }
            }else {
                return null;
            }
        }catch (Exception e) {
            log.error("根据page查询书籍出现错误:e{}",e);
            return null;
        }

        return new PageResult(result,total,pageRequest);
    }

    public String addBook(BookInfo bookInfo) {
        Integer result = null;
        try {
            result = bookInfoMapper.addBook(bookInfo);
            if (result <= 0) {
                log.error("增加图书失败,请练习管理员");
            }
        }catch (Exception e) {
            log.error("增加图书失败:e{}",e);
        }
        return "";
    }

    public BookInfo queryBookInfoById(Integer id) {
        BookInfo bookInfo = null;
        try {
            bookInfo = bookInfoMapper.queryBookInfoById(id);
            return bookInfo;
        }catch (Exception e) {
            log.error("根据ID查询书籍出现错误:e{}",e);
            return null;
        }
    }

    public String updateBook(BookInfo bookInfo) {
        Integer result = null;
        try {
            result = bookInfoMapper.updateBook(bookInfo);
            if (result <= 0) {
                return "更新失败，请联系管理员";
            }
        }catch (Exception e) {
            log.error("updateBook更新出现错误:e{}",e);
            return "更新失败，请联系管理员";
        }
        return "";
    }

    public String deleteBook(Integer id) {
        Integer result = null;
        try {
            result = bookInfoMapper.deleteBook(id);
            if (result <= 0) {
                log.error("书籍删除失败，请联系管理员");
                return "书籍删除失败，请联系管理员";
            }
        }catch (Exception e) {
            log.error("删除图书过程出现错误:e{}",e);
            return "书籍删除失败，请联系管理员";
        }
        return "";
    }

    public String batchDelete(List<Integer> ids) {
        Integer result = null;
        try {
            result = bookInfoMapper.batchDelete(ids);
            if (result <= 0) {
                log.error("批量删除失败:result{}",result);
                return "批量删除失败，请联系管理员";
            }else {
                return "";
            }
        } catch (Exception e) {
            log.error("批量删除失败:e{}",e);
            return "批量删除失败，请联系管理员";
        }
    }
}
